<template>
  <div id="student">
    <!-- 顶部导航 -->
    <el-row class="padding-50">
      <el-col :span="24">
        <ul class="list">
          <li class="logo"><i class="iconfont icon-kaoshi"></i><span>考试系统</span></li>
          <li ><a href="javascript:;" @click="exam()">我的试卷</a></li>
          <li><a href="javascript:;" @click="practice()">我的练习</a></li>
          <li class="logo"><router-link to="/student/examBefore">查看解析</router-link></li>
          <li><router-link to="/student/message">给我留言</router-link></li>
          <li><router-link to="/student">返回首页</router-link></li>
          <li class="right" @mouseenter="flag = true" @mouseleave="flag = false">
            <a href="javascript:;"><i class="iconfont icon-Userselect icon"></i>{{ user.userName }}</a>
            <div class="msg" v-if="flag">
              <p @click="manage()">管理中心</p>
              <p class="exit" @click="exit()">退出</p>
            </div>
          </li>
        </ul>
      </el-col>
    </el-row>

    <!-- 考试列表展示区域 -->
    <div id="examList" style="width: 1150px; margin: 0 auto;">
      <div class="title">我的考试</div>
      <div class="wrapper">
        <ul class="top">
          <li class="order">考试列表</li>
          <li class="search-li">
            <div class="icon">
              <input type="text" placeholder="考试名称" class="search" v-model="examKey" />
              <i class="el-icon-search"></i>
            </div>
          </li>
          <li><el-button type="primary" @click="filterExams">搜索考试</el-button></li>
        </ul>
        <ul class="paper" v-loading="examLoading">
          <li class="item" v-for="item in filteredExamList" :key="item.id" @click="goToExam(item.name,item.course,item.id)">
            <div class="course-card">
              <h3 class="course-title">{{ item.name }}</h3>
              <p class="course-teacher">时长：{{ item.totalTime }}</p>
              <p class="course-desc">{{ item.description }}</p>
              <p class="course-id">考试时间：{{ formatTime(item.startTime) }}</p>
              <p class="course-id">总分：{{ item.totalScore }}</p>
            </div>
          </li>
        </ul>
      </div>
    </div>

    <v-footer></v-footer>
  </div>
</template>

<script>
import myFooter from "@/components/student/myFooter";
import { mapState } from "vuex";

export default {
  components: {
    "v-footer": myFooter
  },
  data() {
    return {
      flag: false,
      user: {},
      examList: [],
      filteredExamList: [],
      examKey: "",
      examLoading: false
    };
  },
  created() {
    this.userInfo();
    this.loadExamList();
  },
  computed: mapState(["isPractice"]),
  methods: {
    exit() {
      this.$router.push({ path: "/" });
      this.$cookies.remove("cname");
      this.$cookies.remove("cid");
    },
    manage() {
      this.$router.push({ path: "/student/manager" });
    },
    userInfo() {
      const studentName = this.$cookies.get("cname");
      const studentId = this.$cookies.get("cid");
      this.user.userName = studentName;
      this.user.studentId = studentId;
    },
    practice() {
      this.$store.commit("practice", true);
      this.$router.push({ path: "/student/startExam" });
    },
    exam() {
      this.$store.commit("practice", false);
      this.$router.push({ path: "/student/exam" });
    },
    loadExamList() {
      this.examLoading = true;
      this.$axios
        .get("/api/exam/page", {
          params: { page: 1, pageSize: 100 }
        })
        .then(res => {
          if (res.data.code === 200) {
            this.examList = res.data.data.records;
            this.filteredExamList = this.examList;
          } else {
            this.$message.error("获取考试失败");
          }
        })
        .catch(() => {
          this.$message.error("请求出错");
        })
        .finally(() => {
          this.examLoading = false;
        });
    },
    filterExams() {
  const key = this.examKey.trim().toLowerCase();
  this.filteredExamList = this.examList.filter(exam =>
    exam.name.toLowerCase().includes(key) ||
    exam.description.toLowerCase().includes(key)
  );
},
    goToExam(name,courseId,examId) {
      this.$router.push({path:`/student/examMsg1/${name}`,query:{courseId:courseId, examId:examId}})
    },
    formatTime(time) {
      return new Date(time).toLocaleString();
    }
  }
};
</script>

<style scoped>
.right .icon {
  margin-right: 6px;
}
#student .padding-50 {
  margin: 0 auto;
  padding: 0 50px;
  box-shadow: 0 0 10px 4px rgba(1,149,255,0.1);
  background-color: #fff;
}
.list a {
  text-decoration: none;
  color: #334046;
}
li {
  list-style: none;
  height: 60px;
  line-height: 60px;
}
#student .list {
  display: flex;
}
#student .list li {
  padding: 0 20px;
  cursor: pointer;
}
#student .list li:hover {
  background-color: #0195ff;
  transition: all 2s ease;
}
#student .list li:hover a {
  color: #fff;
}
#student .list .right {
  margin-left: auto;
  position: relative;
}
#student .list li.right :hover a {
  color: #000;
}
#student .list .logo {
  display: flex;
  font-weight: bold;
  color: #2f6c9f;
}
#student .list .logo i {
  font-size: 50px;
}
.right .msg {
  text-align: center;
  position: absolute;
  top: 60px;
  left: 0px;
  display: flex;
  flex-direction: column;
  border-radius: 2px;
  border-bottom: 3px solid #0195ff;
  background-color: #fff;
}
.right .msg p {
  height: 40px;
  line-height: 40px;
  width: 105px;
}
.right .msg p:hover {
  background-color: #0195ff;
}

/* 考试区域样式复用课程样式 */
.title {
  margin: 20px;
  font-size: 20px;
  font-weight: bold;
  color: #333;
}
.wrapper {
  background-color: #fff;
  padding: 20px;
  border-radius: 8px;
}
.wrapper .top {
  border-bottom: 1px solid #eee;
  margin-bottom: 20px;
  display: flex;
  flex-wrap: wrap;
}
.wrapper .top li {
  margin: 10px 20px;
  display: flex;
  align-items: center;
}
.top .search-li {
  margin-left: auto;
}
.top .search {
  padding: 10px;
  border-radius: 4px;
  border: 1px solid #eee;
}
.top .icon {
  position: relative;
}
.paper {
  display: flex;
  flex-wrap: wrap;
}
.item {
  width: 280px;
  min-height: 180px;
  padding: 16px 20px;
  margin: 15px;
  border-radius: 12px;
  background-color: #ffffff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  cursor: pointer;
  transition: all 0.3s ease;
}
.item:hover {
  transform: translateY(-4px);
  background-color: #0195ff;
  color: #fff;
}
.item:hover .course-title,
.item:hover .course-teacher,
.item:hover .course-desc,
.item:hover .course-id {
  color: #fff;
}
.course-card {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.course-title {
  font-size: 18px;
  font-weight: 600;
  color: #1a1a1a;
  margin: 0;
}
.course-teacher,
.course-id,
.course-desc {
  font-size: 13px;
  color: #666;
  line-height: 1.4;
}
.course-desc {
  max-height: 2.8em;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
</style>
